<?php
/**
 * 邮件简历
 */

namespace app\v1_0\controller\company;

use app\common\logic\ResumeLogic;

class ResumeEmail extends \app\v1_0\controller\common\Base {

    public function _initialize() {
        parent::_initialize();
        $this->checkLogin(1);
        $this->interceptCompanyProfile();
        $this->interceptCompanyAuth();
    }

    public function index() {
        $current_page = input('post.page/d', 1, 'intval');
        $pagesize = input('post.pagesize/d', 10, 'intval');

        $where['company_id'] = ['=', $this->company_profile['id']];
        $list = model('ResumeEmail')->where($where)->select();
        $total = model('ResumeEmail')->where($where)->count();

        $return['items'] = $list;
        $return['total'] = $total;
        $return['current_page'] = $current_page;
        $return['pagesize'] = $pagesize;
        $return['total_page'] = ceil($total / $pagesize);
        $this->ajaxReturn(200, '获取数据成功', $return);
    }

    public function save() {
        $where = [];
        $id = input('post.id/d', 0, 'intval');
        if ($id) {
            $where['id'] = $id;
        }
        $data = [
            'company_id' => $this->company_profile['id'],
            'name' => input('post.name/s', '', 'trim'),
            'pwd' => input('post.pwd/s', '', 'trim'),
            'status' => input('post.status/d', 0, 'intval'),
        ];
        $result = model('ResumeEmail')
            ->validate(true)
            ->allowField(true)
            ->save($data, $where);
        if (false === $result) {
            $this->ajaxReturn(500, model('ResumeEmail')->getError());
        }
        $this->ajaxReturn(200, '保存成功');
    }

    public function delete() {
        $id = input('post.id/d', 0, 'intval');
        $info = model('ResumeEmail')->where(['id' => ['eq', $id], 'company_id' => $this->company_profile['id']])->find();
        model('ResumeEmail')
            ->where([
                'id' => ['eq', $id],
                'company_id' => $this->company_profile['id']
            ])
            ->delete();
        $this->writeMemberActionLog($this->userinfo->uid, '删除绑定邮箱【' . $info->name . '】');
        $this->ajaxReturn(200, '删除成功');
    }

}
